﻿Public Class ProductoVenta
    Public validador As Integer = 0
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            modificar.Enabled = True
            eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True

        End If
    End Sub
    Private Sub ItemventaBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItemventaBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ItemventaBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub ProductoVenta_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.tipoproducto' table. You can move, or remove it, as needed.
            Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Categoría Producto ")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Productos")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
            Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Producto Venta Detalle")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
            Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Producto Venta Encabezado")
        End Try

        PanelDetalle.Enabled = False
        PanelDescripcion.Enabled = False
        Panel2.Enabled = False
        EscompuestoCheckBox.Checked = True
    End Sub

    Private Sub SplitContainer1_Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer1.Panel1.Paint

    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        EscompuestoCheckBox.Checked = True
        EscompuestoCheckBox.CheckState = CheckState.Checked
        PanelDescripcion.Enabled = True
        PanelDetalle.Enabled = True
        PanelEncabezado.Enabled = False
        Mostrar()
        validador = 1

    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        Mostrar()
        PanelDescripcion.Enabled = True
        PanelDetalle.Enabled = True
        PanelEncabezado.Enabled = False
    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try

                ' UnidadescategoriaTableAdapter.DeleteQuery(IdunidadescategoriaTextBox.Text)
                For i As Integer = 0 To GridView1.DataRowCount - 1
                    ItemventadetalleTableAdapter.DeleteQueryItemdetalle(Val(IditemventaTextEdit.Text))
                Next i

                ItemventaTableAdapter.DeleteQueryItem(IditemventaTextEdit.EditValue)
                Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
                Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
                Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
                Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "No se pudo eliminar el registro de la Base de Datos por que esta relacionado con otro campo")
            End Try


        End If
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
        If GridView2.RowCount = 0 Then
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "El Producto Venta Detalle esta vacio, debe de ingresar por lo menos un producto")
        Else

            If PrecioventaTextEdit.EditValue Is DBNull.Value Then
                PrecioventaTextEdit.EditValue = 0.0
            End If
            If PrecioventaexternoTextEdit.EditValue Is DBNull.Value Then
                PrecioventaexternoTextEdit.EditValue = 0.0
            End If
            If CostototalTextEdit.EditValue Is DBNull.Value Then
                CostototalTextEdit.EditValue = 0.0
            End If
            'If validador = 1 Then
            If NombreTextEdit.Text = "" Then
                ErrorProvider1.SetError(NombreTextEdit, "Ingrece un nombre para el Producto Venta")
            Else
                ErrorProvider1.SetError(NombreTextEdit, Nothing)

                If DescripcionTextEdit.Text = "" Then
                    ErrorProvider1.SetError(DescripcionTextEdit, "Ingrece una descripción para el Producto Venta")
                Else
                    ErrorProvider1.SetError(DescripcionTextEdit, Nothing)

                    If IdtipoLookUpEdit.Text = "" Then
                        ErrorProvider1.SetError(IdtipoLookUpEdit, "Seleccione una Categoria del Producto Venta")
                    Else
                        ErrorProvider1.SetError(IdtipoLookUpEdit, Nothing)

                        If PrecioventaTextEdit.EditValue < 1 Then
                            ErrorProvider1.SetError(PrecioventaTextEdit, "Ingrese un Precio Venta Cliente")
                        Else
                            ErrorProvider1.SetError(PrecioventaTextEdit, Nothing)

                            If PrecioventaexternoTextEdit.EditValue < 0 Then
                                ErrorProvider1.SetError(PrecioventaexternoTextEdit, "Ingrese un Precio Venta Visita")
                            Else
                                ErrorProvider1.SetError(PrecioventaexternoTextEdit, Nothing)
                                ItemventaBindingNavigatorSaveItem.PerformClick()
                                Mostrar()
                                Dim id As Integer = IditemventaTextEdit.EditValue
                                PanelDescripcion.Enabled = False
                                PanelDetalle.Enabled = False
                                PanelEncabezado.Enabled = True
                                Try
                                    ItemventaTableAdapter.Fill(DbFireAndIceDataSet.itemventa)

                                Catch ex As Exception
                                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                                    AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
                                End Try
                                IditemventaTextEdit.EditValue = id
                                validador = 2
                            End If
                        End If
                    End If
                End If
            End If
            ' End If


        End If
    End Sub

    Private Sub IditemventaTextEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IditemventaTextEdit.EditValueChanged
        If IditemventaTextEdit.EditValue Is DBNull.Value Then
            IditemventaTextEdit.EditValue = 1
        End If

        Try
            ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, IditemventaTextEdit.EditValue)

        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar el detalle producto venta")
        End Try
    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        If validador = 1 Then

            ItemventaBindingNavigatorSaveItem.PerformClick()
        End If
        Dim id As Integer = IditemventaTextEdit.EditValue
        
        'ItemventadetalleTableAdapter.Fill(DbFireAndIceDataSet.itemventadetalle)
        ProductoVenta2.ShowDialog()

        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.tipoproducto' table. You can move, or remove it, as needed.
            Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Categoría Producto ")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Productos")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
            Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Producto Venta Detalle")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
            Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Producto Venta Encabezado")
        End Try
        Try
            ItemventaTableAdapter.FillByItemVenta(DbFireAndIceDataSet.itemventa, id)
            ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, id)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
        End Try
        CostototalTextEdit.EditValue = 0.0
        For i As Integer = 0 To GridView2.DataRowCount - 1
            ' MsgBox(GridView2.GetRowCellValue(i, "totallinea"))
            CostototalTextEdit.EditValue = GridView2.GetRowCellValue(i, "totallinea") + CostototalTextEdit.EditValue
        Next
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        Mostrar()
        If validador = 1 Then
            For i As Integer = 0 To GridView1.DataRowCount - 1
                Try
                    ItemventadetalleTableAdapter.DeleteQueryItemdetalle(Val(IditemventaTextEdit.Text))

                Catch ex As Exception
                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                    AlertControl1.Show(Owner, "ERROR", "No se pudo eliminar los datos de Detalle")
                End Try
            Next i

            Try
                ItemventaTableAdapter.DeleteQueryItem(IditemventaTextEdit.EditValue)

            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos de Encabezado")
            End Try
            validador = 0
        End If
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.tipoproducto' table. You can move, or remove it, as needed.
            Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Categoría Producto ")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Productos")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
            Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Producto Venta Detalle")
        End Try
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
            Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros de Producto Venta Encabezado")
        End Try
        PanelEncabezado.Enabled = True
        ErrorProvider1.SetError(NombreTextEdit, Nothing)
        ErrorProvider1.SetError(DescripcionTextEdit, Nothing)
        ErrorProvider1.SetError(IdtipoLookUpEdit, Nothing)
        ErrorProvider1.SetError(PrecioventaTextEdit, Nothing)
        ErrorProvider1.SetError(PrecioventaexternoTextEdit, Nothing)
        
        Dim id As Integer = IditemventaTextEdit.EditValue
        Try
            ItemventaTableAdapter.Fill(DbFireAndIceDataSet.itemventa)

        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
        End Try
        IditemventaTextEdit.EditValue = id


    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        If GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "idlinea") Is DBNull.Value Then
        Else
            Dim id As Integer
            id = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "idlinea")
            ' MsgBox(id)
            Try
                ItemventadetalleTableAdapter.DeleteQueryidlinea(id)
                ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, IditemventaTextEdit.EditValue)

            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
            End Try
            CostototalTextEdit.EditValue = 0.0
            For i As Integer = 0 To GridView2.DataRowCount - 1
                ' MsgBox(GridView2.GetRowCellValue(i, "totallinea"))
                CostototalTextEdit.EditValue = GridView2.GetRowCellValue(i, "totallinea") + CostototalTextEdit.EditValue
            Next
        End If
        
    End Sub

    Private Sub ProductoVenta_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

        If validador = 1 And NombreTextEdit.Properties.ReadOnly = False Then
                 Try

                    ' UnidadescategoriaTableAdapter.DeleteQuery(IdunidadescategoriaTextBox.Text)
                Try
                    For i As Integer = 0 To GridView1.DataRowCount - 1
                        ItemventadetalleTableAdapter.DeleteQueryItemdetalle(Val(IditemventaTextEdit.Text))
                    Next i


                    ItemventaTableAdapter.DeleteQueryItem(IditemventaTextEdit.EditValue)
                Catch ex As Exception
                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                    AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
                End Try
                Try
                    Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
                    'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
                    Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
                    'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
                    Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
                    'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
                    Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
                Catch ex As Exception
                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                    AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
                End Try
            Catch ex As Exception

            End Try


            End If

            
    End Sub

    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()
    End Sub

    
End Class